import pymysql

# 1.建立连接
connection = pymysql.connect(host = "localhost",
                             port = 3306,
                             user = "root",
                             password = "123456",
                             database = "test_db",
                             charset = "utf8"
                             )

# 2.建立游标
cursor = connection.cursor()

# 3.执行sql语句, 查询employees表
cursor.execute("select * from employees")

# 4.游标取数据
# 4.1 获取第一条数据
re = cursor.fetchone()
print("第一条数据是：", re)

# 4.2 获取前两条数据
cursor.rownumber = 0 # 游标位置归零
re = cursor.fetchmany(2)
print("前两条数据是：", re)

# 4.3 获取全部数据
cursor.rownumber = 0 # 游标位置归零
re = cursor.fetchall()
print("全部数据是：", re)

# 4.4获取第三条和第四条数据
cursor.rownumber = 2 # 游标位置归零
re = cursor.fetchmany(2)
print("第三条和第四条数据是：", re)

# 5.关闭游标
cursor.close()

# 关闭连接
connection.close()